/* eslint-disable */
import db from './ajaxDb';
import _ from 'lodash';

const Table = 'userData';
/**
 *
 * @returns 用户字段
 * userName: '名字'
 * userImage: '用户头像'
 * userDesc: '用户介绍'
 * userPsw: '用户密码'
 * userTag: '用户标签'
 * userFile: 'user文件数'
 * userDisabled: 0 = 未选中，1 = 选中，2 = 禁用，3 = 删除
 */

// 梳理用户数据
function judgeUserFormDataFS(data) {
    const userDataForm = {
        userName: data.userName,
        userImage: data.userImage,
        userPsw: data.userPsw,
        userDesc: data.userDesc || '',
        userTag: data.userTag || [],
        userFile: data.userFile || 0,
        userDisabled: data.userDisabled || 0,
    };
    return userDataForm;
}

// 获得用户列表
export function getUserData(data) {
    return new Promise((resolve, reject) => {
        try {
            const collection = db.get(Table);
            const list = collection.value();
            resolve({
                code: 200,
                data: _.cloneDeep(list),
            });
        } catch (err) {
            return reject({
                code: 400,
                message: err.message,
            });
        }
    });
}

// 添加用户
export function addUser(document) {
    return new Promise((resolve, reject) => {
        try {
            document.userDisabled = 1;
            const userDataForm = judgeUserFormDataFS(document);
            const collection = db.get(Table);
            const userDataList = collection.value();
            for (let index = 0; index < userDataList.length; index++) {
                const element = userDataList[index];
                if (element.userDisabled === 1) {
                    let userFormData = JSON.parse(JSON.stringify(element));
                    userFormData.userDisabled = 0;
                    replaceUserById(element.id, userFormData);
                }
            }
            const model = collection.insert(userDataForm).write();
            resolve({
                code: 200,
                data: _.cloneDeep(model),
            });
        } catch (err) {
            return reject({
                code: 400,
                message: err.message,
            });
        }
    });
}

// 修改用户
export function replaceUserById(id, attrs) {
    return new Promise((resolve, reject) => {
        try {
            attrs = judgeUserFormDataFS(attrs);
            const collection = db.get(Table);
            const model = collection
                .find({ id: id })
                .assign(attrs)
                .write();

            resolve({
                code: 200,
                data: _.cloneDeep(model),
            });
        } catch (err) {
            return reject({
                code: 400,
                message: err.message,
            });
        }
    });
}

// 删除用户
export function deleteUserById(id) {
    return new Promise((resolve, reject) => {
        try {
            const collection = db.get(Table);
            collection.removeById(id).write();
            resolve({
                code: 200,
            });
        } catch (err) {
            return reject({
                code: 400,
                message: err.message,
            });
        }
    });
}
